import { managerApi } from "@/api/bpm/task/manager";
import { BasicColumn, BasicTable, FormSchema, useTable } from "@/components/Table";
import { Button, Tag } from "antd";
import { useDrawer } from "@/components/Drawer";
import { useTranslation } from "react-i18next";

import HandlingProcess from "../todo/components/handlingProcess";
import { formatDate } from "@/utils/util";
const index = () => {
	const { t } = useTranslation();
	const todo_en_vi = "task.todo";
	const en_vi = "task.taskManager";
	const formatTime = (timestamp: any) => {
		if (!timestamp) return `0 ${t(`${todo_en_vi}.second`)}`;
		const seconds = parseInt((timestamp / 1000) % 60);
		const minutes = parseInt((timestamp / 60000) % 60);
		const hours = parseInt((timestamp / 3600000) % 60);
		if (hours) return `${hours}${t(`${todo_en_vi}.hour`)}${minutes}${t(`${todo_en_vi}.minute`)}`;
		if (minutes) return `${minutes}${t(`${todo_en_vi}.minute`)}`;
		if (seconds) return `${seconds}${t(`${todo_en_vi}.second`)}`;
	};
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.taskName`),
			field: "name",
			component: "Input"
		},
		{
			label: t(`${en_vi}.createTime`),
			field: "createTime",
			component: "RangePicker"
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.process`),
			dataIndex: "processInstance",
			render: (processInstance: any) => processInstance.name
		},
		{
			title: t(`${en_vi}.initiator`),
			dataIndex: "processInstance",
			render: (processInstance: any) => processInstance.startUser.nickname
		},
		{
			title: t(`${en_vi}.initiationTime`),
			dataIndex: "createTime",
			render: (time: any) => {
				return formatDate(new Date(time));
			}
		},
		{
			title: t(`${en_vi}.currentTask`),
			dataIndex: "name"
		},
		{
			title: t(`${en_vi}.taskStartTime`),
			dataIndex: "createTime",
			render: (time: any) => {
				return formatDate(new Date(time));
			}
		},
		{
			title: t(`${en_vi}.taskEndTime`),
			dataIndex: "endTime",
			render: (time: any) => {
				return formatDate(new Date(time));
			}
		},
		{
			title: t(`${en_vi}.approver`),
			dataIndex: "assigneeUser",
			render: (assigneeUser: any) => {
				return assigneeUser.nickname;
			}
		},
		{
			title: t(`${en_vi}.approvalStatus`),
			dataIndex: "status",
			render: (status: any) => {
				if (status == 2) {
					return <Tag color="success">{t(`${en_vi}.approved`)}</Tag>;
				} else {
					return <Tag color="default">{t(`${en_vi}.canceled`)}</Tag>;
				}
			}
		},
		{
			title: t(`${en_vi}.approvalComment`),
			dataIndex: "reason"
		},
		{
			title: t(`${en_vi}.timeConsumed`),
			dataIndex: "durationInMillis",
			render: (time: any) => {
				return formatTime(time);
			}
		},
		{
			title: t(`${en_vi}.processInstanceId`),
			dataIndex: "processInstanceId"
		},
		{
			title: t(`${en_vi}.taskId`),
			dataIndex: "processInstanceId"
		},
		{
			title: t(`${en_vi}.operations`),
			fixed: "right",
			width: 200,
			render: (_, record) => {
				return (
					<div>
						<Button type="link" onClick={handle(record)}>
							{t(`${en_vi}.history`)}
						</Button>
					</div>
				);
			}
		}
	];
	const [register] = useTable({
		api: managerApi.managerPage,
		columns,
		useSearchForm: true,
		formConfig: {
			labelWidth: 150,
			schemas,
			showActionButtonGroup: true,
			baseColProps: { span: 7 },
			actionColOptions: { span: 24 }
		},
		bordered: true
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const handle = (record: any) => () => {
		openDrawer(true, {
			record
		});
	};
	return (
		<div>
			<div className=" page-content">
				<BasicTable register={register}></BasicTable>
			</div>
			<HandlingProcess register={registerDrawer}></HandlingProcess>
		</div>
	);
};
export default index;
